package com.trajan.exercise.algorithm;

/*

   Created with IntelliJ IDEA.

   Ing. Tomáš Herich
   --------------------------- 
   18. 06. 2013
   4:14 PM

*/

public class SortAlgorithms {


    private int[] myArray;
    private int arraySize;

    public static void main(String[] args) {

        SortAlgorithms sort = new SortAlgorithms();
        
        sort.printArray();

        sort.bubbleSort();

        System.out.println("\n\n Bubble sorted array");

        sort.printArray();

    }

    public SortAlgorithms() {

        arraySize = 20;
        myArray = new int[50];

        for (int i = 0; i < arraySize; i++) {
            myArray[i] = (int) (Math.random() * 20);
            //myArray[i] = arraySize - i;
        }


    }
    

    public void bubbleSort() {


        for (int i = arraySize; i > 0; i--) {

            for (int j = 0; j < i; j++) {

                if (myArray[j] > myArray[j+1]) {

                    swap(j, j+1);

                }
            }
        }
    }

    private void swap(int firstIndex, int secondIndex) {
        int temp = myArray[firstIndex];
        myArray[firstIndex] = myArray[secondIndex];
        myArray[secondIndex] = temp;
    }

    public void  selectionSort() {


    }

    public void insertionSort() {


    }

    public void quickSort() {



    }


    public void printArray() {

        for (int i = 0; i <= arraySize; i++) {
            System.out.print("-----");
        }
        System.out.println();

        for (int i = 0; i <= arraySize; i++) {
            if (i < 10) {
                System.out.print("  " + i + " |");
            } else  {
                System.out.print(" " + i + " |");
            }
        }
        System.out.println();

        for (int i = 0; i <= arraySize; i++) {
            System.out.print("-----");
        }
        System.out.println();

        for (int i = 0; i <= arraySize; i++) {
            if (myArray[i] < 10) {
                System.out.print("  " + myArray[i] + " |");
            } else  {
                System.out.print(" " + myArray[i] + " |");
            }
        }
        System.out.println();

        for (int i = 0; i <= arraySize; i++) {
            System.out.print("-----");
        }

    }
}
